
/**
 * Given two integer arrays nums1 and nums2, return an array of their intersection.
 * Each element in the result must be unique and you may return the result in any
 * order.
 * <p>
 * <p>
 * Example 1:
 * <p>
 * <p>
 * Input: nums1 = [1,2,2,1], nums2 = [2,2]
 * Output: [2]
 * <p>
 * <p>
 * Example 2:
 * <p>
 * <p>
 * Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
 * Output: [9,4]
 * Explanation: [4,9] is also accepted.
 * <p>
 * <p>
 * <p>
 * Constraints:
 * <p>
 * <p>
 * 1 <= nums1.length, nums2.length <= 1000
 * 0 <= nums1[i], nums2[i] <= 1000
 * <p>
 * <p>
 * Related Topics 数组 哈希表 双指针 二分查找 排序 👍 663 👎 0
 */


package com.xixi.basicAlgroithms.hash;

import java.util.HashSet;
import java.util.Set;

public class ID00349IntersectionOfTwoArrays {
    public static void main(String[] args) {
        Solution solution = new ID00349IntersectionOfTwoArrays().new Solution();
    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {


        public int[] intersection(int[] nums1, int[] nums2) {

            boolean[] existsInNums1 = new boolean[1000];
            boolean[] resultNums = new boolean[1000];
            for (int i : nums1) {
                existsInNums1[i] = true;
            }
            for (int i2 : nums2) {
                if (existsInNums1[i2]) resultNums[i2] = true;
            }
            Set<Integer> resSet = new HashSet<>();

            for (int ir = 0; ir < 1000; ir++) {
                if (resultNums[ir]) resSet.add(ir);
            }

            int[] res = new int[resSet.size()];
            int a = 0;
            for (Integer integer : resSet) {
                res[a] = integer;
                a++;
            }

            return res;


        }
    }
//leetcode submit region end(Prohibit modification and deletion)


}